from lib2to3.pgen2 import driver
from selenium import webdriver

xp  = 'xpath'
class BasePage:

    #__init__
    def __init__(self,driver):
        self.driver = driver
        self.driver.implicitly_wait(10)

    #get
    def open(self,url):
        self.driver.get(url)
    
    #locate 对元素进行定位
    def locate(self,name,value):
        return self.driver.find_element(name,value)
    
    #input
    def input(self,name,value,txt):
        self.locate(name,value).send_keys(txt)

    #click
    def click(self,name,value):
        self.locate(name,value).click()

    #定位验证码图片
    def img(self,name,value):
        self.locate(name,value).get_attribute('src')

    #sleep
    def sleep(self,time):
        self.sleep(time)

    #wait
    def wait(self,time):
        self.wait(time)

    #quit
    def quit(self):
        self.driver.quit()
        
    #clear
    def clear(self,name,value):
        self.locate(name,value).clear()
        
    #get文本
    def get(self,name,value):
        self.locate(name,value).getText()



    #获取输入框的文本值
    def attribute(self,name,value,src):
        self.locate(name,value).get_attribute(src)
        
    #时间控件选择时间
    def choosetime(self,startime,endtime):
        stattimepath =  '//td[@title="'+startime+'"]'    #组合开始时间
        endtimepath = '//td[@title="'+endtime+'"]'       #组合结束时间
        self.click(xp,'//input[@placeholder="开始日期"]')
        self.click(xp,stattimepath)           
        self.click(xp,endtimepath)

    #类型判断
    def TypeChoose(self,type):
        #判断类型    
        if type == '送储值' or '送积分':
            if type == '送积分' :
                self.click(xp,'//li[normalize-space(text())="送积分"]')
            else :
                pass
        else :
            if type == '送抽奖机会':
                self.click(xp,'//li[normalize-space(text())="送抽奖机会"]')
            elif type == '送兑换券':
                self.click(xp,'//li[normalize-space(text())="送兑换券"]')
            elif type == '送代金券':
                self.click(xp,'//li[normalize-space(text())="送代金券"]')
            else :
                self.click(xp,'//li[normalize-space(text())="送满减券"]')

    #上架
    def isON(self,ButtonXpath):
        self.click(xp,ButtonXpath)  #ButtonXpath  上加按钮的位置

    #点击新增
    def new(self,):
        try:
            self.click(xp,'//span[text()="新增"]/../..')
        except:
            self.click(xp,'//span[text()="添加"]/../..')